<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GestorTelefono
 *
 * @author Karla
 */
include_once("../../util/Cadena.php");
include_once("../../comunicacion/db/MySQL.php");

class GestorTelefono {

    public function buscarTelefono($numeroTelefono,$maxRows) {
	$campoSelect;
	$campoWhere;

	$cadena = new Cadena();
	$listaPalabrasSimilares = $cadena->separarEnSimilares($numeroTelefono);
	foreach ($listaPalabrasSimilares as $palabraSimilar) {
	    $campoSelect[] = "if(INSTR(f.fono,'" . $palabraSimilar["palabra_similar"] . "')<>0," . $palabraSimilar["ponderacion"] . ",0)";
	    $campoWhere[] = "f.fono like ('%" . $palabraSimilar["palabra_similar"] . "%')";
	}
	$query = "select f.*,
		" . implode(" + ", $campoSelect) . " 
		as ponderacion
		from fono f
		where (" . implode(" or ", $campoWhere) . ")
		order by ponderacion desc
		limit $maxRows
		";
	$query2 = "select count(*) as conteo
		    from fono f
		    where (" . implode(" or ", $campoWhere) . ")
		    ";
	$myqsql = new MySQL();
	$conexion = $myqsql->getConexion();
	$result = $conexion->query($query);
	$result2 = $conexion->query($query2);
//	echo $query2;
	$row = $result2->fetch_assoc();
	$conteo=$row["conteo"];
	$conexion->close();

	return array("result" => $result, "conteo"=> $conteo);
    }

}

?>
